package com.glory.brepda.utils;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.Environment;

import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StaticMembers;

import java.io.File;

public class FileUtils {

    // 判断文件是否存在
    public static boolean fileIsExists(String strFile) {
        try {
            File f = new File(strFile);
            return f.exists();
        } catch (Exception e) {
            return false;
        }
    }

    // 判断是否是 Android Q（API 29）及以上
    public static boolean isAndroidQ() {
        return Build.VERSION.SDK_INT >= 29;
    }

    /**
     * 获取下载数据路径
     */
    public static String getDownloadDataPath(Context context) {
        if (isAndroidQ()) {
            File dir = context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
            return (dir != null ? dir.getAbsolutePath() : "") + File.separator + "LJDATAS" + File.separator;
        } else {
            return FreeApi_StaticMembers.ROOT_PATH + "/LJDATAS/";
        }
    }

    /**
     * 获取本地文件下载地址
     */
    public static String getLocalPathAndFileName(Context context) {
        return getDownloadDataPath(context);
    }

    /**
     * 判断文件是否是图片
     */
    public static boolean checkImage(String path) {
        if (path.endsWith("jpg") || path.endsWith("png") || path.endsWith("jpeg")) {
            return true;
        } else {
            return BitmapFactory.decodeFile(path) != null;
        }
    }
}
